﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace IntelliTouchMix
{
    public partial class ComponentBox : Form
    {
        Animation anim;
        List<entryCategory> lCategories;
        int currentPosFather;
        // List<entryComponent> lElements;
        String componentName;
        public static Image errorImage;
        public static Image emptyImage;
        public ComponentBox()
        {
            InitializeComponent();
           /* anim = new Animation();
            imgCenter.setDataLeftAnim();
            anim.addAnimation(imgCenter);*/
            
            emptyImage = Image.FromFile("../carrusel/empty.png");
            errorImage = Image.FromFile("../carrusel/error.png");
            lCategories = new List<entryCategory>();
            // lElements = new List<entryComponent>();
            test();
        }
        void test()
        {
            // Create Categories
            addToComponents(IdCategory.effects, IdCategory.none, "Samples", "../carrusel/sample1.png", 0);
            addToComponents(IdCategory.velocidad, IdCategory.none,"Velocity", "pachucos.jpg", 0);
            addToComponents(IdCategory.volume, IdCategory.none, "Volume", "pachucos.jpg", 0);
            addToComponents(IdCategory.effects2, IdCategory.none, "Samples plus", "pachucos.jpg", 0);
            // Create Components
            addToComponents(CategoryId.newId(), IdCategory.effects, "Tania", "../carrusel/sample1.png", 0);
            addToComponents(CategoryId.newId(), IdCategory.effects, "Rodrigo", "pachucos.jpg", 0);
            addToComponents(CategoryId.newId(), IdCategory.effects, "Carlos", "pachucos.jpg", 0);
            addToComponents(CategoryId.newId(), IdCategory.effects, "Normita", "pachucos.jpg", 0);
            addToComponents(CategoryId.newId(), IdCategory.effects, "Manuel", "pachucos.jpg", 0);
            //
            addToComponents(CategoryId.newId(), IdCategory.effects2, "auuuuu", "pachucos.jpg", 0);
            addToComponents(CategoryId.newId(), IdCategory.effects2, "brrr", "pachucos.jpg", 0);
            addToComponents(CategoryId.newId(), IdCategory.effects2, "nya", "pachucos.jpg", 0);
            addToComponents(CategoryId.newId(), IdCategory.effects2, "jojo", "pachucos.jpg", 0);
            fillFormCategory();
            fillFormElement();
        }

        public void addToComponents(IdCategory id, IdCategory idFather, String name, String img, int value)
        {
            if (idFather == IdCategory.none) // master category
            {
                lCategories.Add(new entryCategory(id, name, img, value));
                currentPosFather = lCategories.Count / 2;  // Always point to the middle
            }
            else
            {// its a son
                entryCategory father = findFather(idFather);
                if (father == null)
                {// didn't find it in the list
                    MessageBox.Show("Error Grave no encontro El padre del Elemento"+ id+" "+name );
                    return;// error
                }
                father.addSon(new entryElement(id, name, img, value));
                father.currentSonIndex = father.numSons() / 2;  // Always point to the middle
            }
        }

        private entryCategory findFather(IdCategory idFather)
        {
            //entryCategory t;
            // iterate through lCategories and find the id
            foreach (entryCategory t in lCategories)
            {
                if(t.id == idFather)
                return t;
            }
            return null;
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            // coloca el nombre del componente y actualiza la imagen de arriba como las de abajo
            // asi como el nombre de abajo
            if (currentPosFather < lCategories.Count - 1)
            {
                currentPosFather++;
                fillFormCategory();
                fillFormElement();
            }  
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            if (currentPosFather > 0)
            {
                currentPosFather--;
                fillFormCategory();
                fillFormElement();

            } 
        }

        private void buttonX4_Click(object sender, EventArgs e)
        {
            entryCategory f = lCategories[currentPosFather];
            if (f.currentSonIndex < f.getSons().Count - 1)
            {
                f.currentSonIndex++;
                fillFormElement();
            }
            
        }

        private void buttonX3_Click(object sender, EventArgs e)
        {
            entryCategory f =  lCategories[currentPosFather];
            if (f.currentSonIndex > 0)
            {

               f.currentSonIndex--;
                fillFormElement();
            }
        }
        void fillFormCategory()
        {
            entryCategory p= lCategories[currentPosFather];
            String n = p.name;
            categoryName.Text = "<b><font size=\"+6\"><font color=\"#B02B2C\"> " + n.Substring(0, 1) + "</font>" + n.Substring(1) + " </font></b>";
            
            if (currentPosFather - 1 == -1 )
                imgLeft.Image = ComponentBox.emptyImage;
            else
                imgLeft.Image = lCategories[currentPosFather - 1].img;
            
            if(currentPosFather+1 == lCategories.Count)
                imgRight.Image = ComponentBox.emptyImage;
            else
                imgRight.Image = lCategories[currentPosFather + 1].img;

            imgCenter.Image = p.img;
        }
        void fillFormElement()
        {
            int currentPosSon = lCategories[currentPosFather].currentSonIndex;
            List<entryElement> sons = lCategories[currentPosFather].getSons();
            if (currentPosSon < 0 || sons.Count < 1 )
            {
                clearFormElement();
                return;
            }
            entryElement son = sons[currentPosSon];
            String n = son.name;
            elementName.Text = "<b><i><font size=\"+6\"><font color=\"#B02B2C\"> " + n.Substring(0, 1) + "</font>" + n.Substring(1) + " </font></i></b>";

            if (currentPosSon - 1 == -1)
                imgSonLeft.Image = ComponentBox.emptyImage;
            else
                imgSonLeft.Image = sons[currentPosSon - 1].img;

            if (currentPosSon + 1 == sons.Count)
                imgSonRight.Image = ComponentBox.emptyImage;
            else
                imgSonRight.Image = sons[currentPosSon + 1].img;

            imgSonCenter.Image = son.img;
        }
        void clearFormElement()
        {
            elementName.Text = "";
            imgSonLeft.Image = ComponentBox.emptyImage;
            imgSonRight.Image = ComponentBox.emptyImage;
            imgSonCenter.Image = ComponentBox.emptyImage;
        }
    }
    public class entryCategory
    {
        public IdCategory id;
        List<entryElement> lSons;
        public int currentSonIndex = -1;
        public String name; 
        // String Img;
        public Image img;
        int value;
        public entryCategory(IdCategory id1,  String name1, String img1, int value1)
        {
            id = id1;
            name = name1;
            value=value1;
            lSons = new List<entryElement>();
            try
            {
                img = Image.FromFile(img1);
            }
            catch (Exception e)
            {
                img = ComponentBox.errorImage;
                // MessageBox.Show("Image: "+name1+" Not found");
            }
        }
        public List<entryElement> getSons() { return lSons; }
        public int numSons() { return lSons.Count; }
        public void addSon(entryElement eSon)
        {
            eSon.idFather = this;
            lSons.Add(eSon);
        }
    }
    public class CategoryId
    {
        static int nextId = 0;
        public static IdCategory newId()
        { return (IdCategory)nextId++; }
    }
    public class entryElement
    {
        IdCategory id;
        public entryCategory idFather;
        public String name;
        public Image img;
        int value;
        public entryElement(IdCategory id1, String name1, String img1, int value1)
        {
            id = id1;
            name = name1;
            value=value1;
            try
            {
                img = Image.FromFile(img1);
            }
            catch (Exception e)
            {
                img = ComponentBox.errorImage;
                // MessageBox.Show("Image: "+name1+" Not found");
            }
        }
    }
    public enum IdCategory
    {
        volume, velocidad, effects,effects2,none
    }
}
